/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionabsence.action;

import fr.p10.miage.gestionabsence.model.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author seidso
 */
public class DeleteAbsenceNowIntervenantAction extends org.apache.struts.action.Action {

/*
     * forward name="success" path=""
     */
    private static final String success = "listeEleveAbsenceIntervenant";
    private static final String fail = "listeCoursIntervenant";
    private static final String exit = "Index";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        if(SessionUtilisateur.isAuthenticated(request)){
            Intervenant intervenant  = SessionUtilisateur.getIntervenant(request); 
            System.out.println("test "+request);
            int idAgenda = Integer.parseInt(request.getParameter("ida"));
            int idEtudiant = Integer.parseInt(request.getParameter("ide"));
            if(idAgenda!=0 && idEtudiant!=0){
                Agenda agenda =  AgendaBDD.trouverParId(idAgenda);
                if(agenda!=null && agenda.getIdI()==intervenant.getIdI()){
                    Etudiant etudiant=EtudiantBDD.trouverParId(idEtudiant);
                    if(etudiant!=null && etudiant.getId()!=0){
                        Presence presence=PresenceBDD.trouverParEtudiantetAgenda1(etudiant.getId(),agenda.getIdCr());
                        if(presence!=null && presence.getId()!=0){
                            PresenceBDD.supprimer(presence.getId());
                            request.setAttribute("id", agenda.getIdCr());
                            return mapping.findForward(success);
                        }
                    }
                    else{
                        request.setAttribute("id", agenda.getIdCr());
                        return mapping.findForward(success);
                    }
                }
                else{
                    return mapping.findForward(fail);
                }
            }
            else{
                return mapping.findForward(fail);
            }
        }
     
            return mapping.findForward(exit);
       
    }
}
